<template>
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
        @click="changeBrand(item.id)"
        :class="{active:item.id ==filterData.brands.selectedBrand}"
          href="javascript:;"
          v-for="(item, index) in filterData.brands"
          :key="index"
          >{{ item.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{item.name}}</div>
      <div class="body">
        <a
        @click="changeProp(item,i.id)"
        :class="{active:i.id ==item.selectedAttr}"
        href="javascript:;" v-for="i in item.properties" :key="i.id">{{
          i.name
        }}</a>
      </div>
    </div>
  </div>
</template>
<script>
</script>
<script setup>
import { findSubCategoryFilter } from '@/api/category'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
export default {
  name: 'SubFilter'

}

// 1. 获取数据
// 2. 数据中需要全部选中，需要预览将来点击激活功能。默认选中全部
// 3. 完成渲染
const route = useRoute()
const filterData = ref(null)
const filterLoading = ref(null)
const props = defineProps({
  rightMenuStyle: String,
  rightMenu: String
})
const emit = defineEmits(['filterchange'])

watch(
  () => route.params.id,
  (newVal) => {
    if (newVal && `/category/sub/${newVal}` == route.path) {
      filterLoading.value = true
      // 发请求
      findSubCategoryFilter(route.params.id).then((data) => {
        // 1.品牌：
        data.result.brands.unshift({ id: null, name: '全部' })
        // 属性
        // 每组数据加入是否选中标志
        data.result.brands.selectedBrand = null
        data.result.saleProperties.forEach((item) => {
          item.selectedAttr = null
          item.properties.unshift({ id: null, name: '全部' })
        })
        // 设置修改的数据
        filterData.value = data.result
        filterLoading.value = false
      })
    }
  },
  { immediate: true }
)
// 获取筛选参数
const getFilterParams = () => {
  const obj = { brandId: null, attrs: [] }
  // 品牌
  obj.brandId = filterData.value.brands.selectedBrand
  filterData.value.saleProperties.forEach(item => {
    if (item.selectedAttr) {
      const prop = item.properties.find(prop => prop.id === item.selectedAttr)
      obj.attrs.push({ group: item.name, propertyName: prop.name })
    }
  })
  if (obj.attrs.length === 0) obj.attrs = null
  return obj
}

// 记录当前品牌
const changeBrand = (brandId) => {
  if (brandId == filterData.value.brands.selectedBrand) return
  filterData.value.brands.selectedBrand = brandId
  emit('filterchange', getFilterParams())
}
// 记录选择的销售属性
const changeProp = (item, propId) => {
  if (propId == item.selectedAttr) return
  item.selectedAttr = propId,
  emit('filterchange', getFilterParams())
}

</script>

<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
